<template>
  <div class="visit-date">
    <p>参观日期</p>
    <p>
      <span>
        {{ dateFormat(order.visitDate, "YYYY-M-D") }}
        &nbsp;&nbsp;
        {{ order.visitPeriod }}
      </span>
    </p>
  </div>
  <div class="team-info">
    <p>团体信息</p>
    <div class="info-box">
      <van-form ref="ruleFormRef" @submit="onSubmit">
        <van-cell-group inset>
          <van-field
            v-model="form.teamName"
            name="teamName"
            label="团队名称"
            placeholder="请填写团队名称"
            :rules="[{ required: true, message: '请填写团队名称' }]"
          />
          <van-field
            v-model="form.peopleNum"
            name="peopleNum"
            label="团队人数"
            type="number"
            placeholder="请填写团队人数"
            :rules="[
              { required: true, message: '请填写团队人数' },
              { validator: validatePeopleNum, message: `团队人数不能超过剩余票数${order.ticketNum}张` },
              { validator: validateMinPeopleNum, message: '团队人数不能少于10人' }
            ]"
          />
          <van-field
            v-model="form.leaderName"
            name="leaderName"
            label="领队姓名"
            placeholder="请填写领队姓名"
            :rules="[{ required: true, message: '请填写领队姓名' }]"
          />
          <van-field
            v-model="form.leaderPhone"
            name="leaderPhone"
            label="领队电话"
            placeholder="请填写领队电话"
            :rules="[
              { required: true, message: '请填写领队电话' },
              {
                pattern: /^1(3|4|5|6|7|8|9)\d{9}$/,
                message: '请输入正确的手机号码',
              },
            ]"
          />
          <van-radio-group
            inset
            v-model="form.isExplain"
            name="isExplain"
            label="序厅讲解"
            direction="horizontal"
          >
            <van-cell title="序厅讲解" clickable>
              <template #right-icon>
                <van-radio :name="0">不需要</van-radio>
                <van-radio :name="1">需要</van-radio>
              </template>
            </van-cell>
          </van-radio-group>
        </van-cell-group>
        <div class="page-bottom">
          <van-checkbox v-model="checked">
            <span class="tip">我已阅读并同意</span>
            <span class="tip desc" @click.stop="showMessage"
              >《数据隐私安全声明》</span
            >
          </van-checkbox>
          <div class="button-group">
            <van-button type="default" @click="router.back()" size="large" round
              >返回</van-button
            >
            <div style="width: 30px"></div>
            <van-button type="primary" native-type="submit" size="large" round
              >下一步</van-button
            >
          </div>
        </div>
      </van-form>
    </div>
  </div>
  <Tip ref="refTip" />
  <Statement ref="refStatement" />
</template>

<script setup lang="ts">
import { ref, reactive, onBeforeMount, onMounted, toRaw } from "vue";
import router from "@/router";
import { useOrderStore } from "@/stores/order";
import { dateFormat } from "@/utils/dateFormat";
import { type OrderDTO } from "@/interface/order";
import { showNotify } from "vant";
import Tip from "./components/tip.vue";
import Statement from "./components/statement.vue";

const refTip = ref();
const refStatement = ref();
const order = useOrderStore().orderGetter;
const checked = ref(false);
const form = reactive<OrderDTO>({
  isExplain: 0,
  leaderName: null,
  leaderPhone: null,
  peopleNum: 0,
  platform: null,
  ptId: 2,
  teamName: null,
  tickerId: null,
  visitDate: null,
  visitPeriod: null,
});
const ruleFormRef = ref();

const showMessage = () => {
  refStatement.value.showDialog();
  return false;
};

const onSubmit = () => {
  if (!checked.value) {
    showNotify({ type: "danger", message: "请阅读并勾选数据隐私安全声明" });
    return;
  }
  useOrderStore().setOrder(form);
  router.push("/flow/confirminfo");
};

const validatePeopleNum = (val: string) => {
  // 确保输入的是数字，并且不超过剩余票数
  const num = Number(val);
  const maxNum = order.ticketNum || 0;
  return !isNaN(num) && num > 0 && num <= maxNum;
};

const validateMinPeopleNum = (val: string) => {
  // 确保团队人数大于等于10
  const num = Number(val);
  return !isNaN(num) && num >= 10;
};

onBeforeMount(() => {
  const data: OrderDTO = toRaw(order);
  let key: keyof OrderDTO;
  for (key in form) {
    form[key] = data[key];
  }

});

onMounted(() => {
  refTip.value.showDialog();
});
</script>

<style lang="scss" scoped>
.visit-date {
  margin: 10px;

  p:first-child {
    color: #000;
    font-size: 16px;
    font-weight: 600;
  }

  p:last-child {
    font-size: 16px;
    line-height: 28px;
  }
}

.team-info {
  margin: 10px;

  > p {
    color: #000;
    font-size: 16px;
    font-weight: 600;
    line-height: 28px;
  }

  .info-box {
    width: 100%;
  }

  .tip {
    font-size: 14px;
    color: #282828;

    &.desc {
      color: var(--base-color);
    }
  }
}

.page-bottom {
  width: 360px;
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 20px;
  overflow: hidden;

  .button-group {
    margin-top: 15px;
    display: flex;
    justify-content: center;
  }
}
</style>
